//
//  NewPlatformView.swift
//  d11
//
//  Created by peale_pro on 2019/6/27.
//  Copyright © 2019年 Tech_001. All rights reserved.
//

import UIKit
typealias NewPlatformSeltBlock = (_ dict: [String:Any],_ index:Int) -> Void
class NewPlatformView: UIView,UICollectionViewDelegate,UICollectionViewDataSource {
    var arrayList : [[String:Any]]? //数据列表
    var type : Int = 0
    var selectIndex = 0
    var selectDict : [String:Any]?
    var bgImageView : UIImageView?
//    var lineView1 :UIView?
//    var lineView2 :UIView?
//    var lineView3 :UIView?
//    var lineView4 :UIView?
//    var lineViewY1 :UIView?
//     var lineViewY2 :UIView?
//     var lineViewY3 :UIView?
    var topBtn : UIButton?
    var topImageView : FLAnimatedImageView?
    var bottomBtn :UIButton?
    var bottomImageView : FLAnimatedImageView?
    var newPlatformSeltBlock: NewPlatformSeltBlock?
    func callClickBlock(_ block: @escaping NewPlatformSeltBlock) {
        newPlatformSeltBlock = block
    }
    var isFirstRealod  = false
    lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: (self.width-10-12)/2, height: self.height/3)
        layout.sectionInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        layout.minimumInteritemSpacing = 0
        //  }
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .vertical
        // layout.headerReferenceSize = CGSize.init(width: (kScreenWidth-20)/4, height: 87.5)
        // 设置分区头视图和尾视图宽高
        //    layout.headerReferenceSize = CGSize.init(width: kScreenWidth, height: 80)
        //    layout.footerReferenceSize = CGSize.init(width: kScreenWidth, height: 80)
        
        let collView = UICollectionView.init(frame: CGRect(x:6, y:0, width:self.width-10-12, height:self.height), collectionViewLayout: layout)
        
        if collView.responds(to:#selector(setter: UIView.layoutMargins)) {
            collView.layoutMargins = .zero;
        }
        
        collView.autoresizingMask = UIView.AutoresizingMask.flexibleWidth.union(UIView.AutoresizingMask.flexibleHeight)
        collView.register(NewHomePlatformItemCell.self, forCellWithReuseIdentifier: "NewHomePlatformItemCell")
        //        collView.register(HelperListHeadView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HelperListHeadView1")
        collView.showsVerticalScrollIndicator = false
        collView.showsHorizontalScrollIndicator = false
        collView.backgroundColor = UIColor.clear
        collView.delegate = self
        collView.dataSource = self
        // collView.backgroundColor = UIColor.red
        return collView
    }()
    
    convenience init(list:[[String:Any]],frame:CGRect) {
        self.init(frame: frame)
        self.setUpUI()
    }
    
    func setUpUI() -> Void {
        self.arrayList = []
        let platforms = GlobalInfo.platformInfoData
        self.arrayList =  platforms
        self.backgroundColor = UIColor.clear
        
        self.bgImageView = UIImageView.init()
        self.bgImageView?.isUserInteractionEnabled = true
        self.bgImageView?.image =
        imageNamed("icon_home_platform_bg")?
            .resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 15, bottom: 5, right: 15),
                            resizingMode: .stretch)
        self.addSubview(self.bgImageView!)
        self.bgImageView?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-10)
            make.height.equalToSuperview()
            make.left.equalToSuperview()
            make.top.equalToSuperview()
        }
        self.bgImageView?.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints{(make) in
            //make.right.equalTo((self.rightBtn?.snp.left)!).offset(-6)
            make.height.equalTo(self.height)
           // make.width.equalToSuperview().offset(-10)
            make.width.equalTo(self.width-10-12)
            make.top.equalToSuperview()
            make.left.equalTo((self.bgImageView?.snp.left)!).offset(6)
        }
        
        self.topBtn = UIButton.init(type: .custom)
        // self.topBtn?.setImage(imageNamed("home_platform_top"), for: .normal)
        // self.topBtn?.backgroundColor = UIColor.white
        self.topBtn?.isHidden = true
        self.topBtn?.addTarget(self, action: #selector(actionToScrollTop), for: UIControl.Event.touchUpInside)
        self.bgImageView?.addSubview(self.topBtn!)
        self.topBtn?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-12)
            make.height.equalTo(24)
            make.centerX.equalToSuperview()
            make.top.equalTo(3)
        }
        
        self.topImageView = FLAnimatedImageView.init()
        let url =  Bundle.main.url(forResource: "home_platform_top_gif", withExtension: "gif")
        do{
            let data = try Data(contentsOf: url!)
            let animatedImage = FLAnimatedImage.init(animatedGIFData: data)
            self.topImageView?.animatedImage = animatedImage
        }catch{}
        
        self.self.topBtn?.addSubview(self.topImageView!)
        self.topImageView?.snp.makeConstraints{(make) in
            make.width.equalTo(25)
            make.height.equalTo(14)
            make.centerX.equalToSuperview()
            make.top.equalTo(6)
        }
        self.bottomBtn = UIButton.init(type: .custom)
        // self.bottomBtn?.setImage(imageNamed("home_platform_bottom"), for: .normal)
        //  self.bottomBtn?.setImage(UIImage.gif(name: "home_platform_bottom_gif"), for: .normal)
        self.bottomBtn?.addTarget(self, action: #selector(actionToScrollBottom), for: UIControl.Event.touchUpInside)
        //   self.bottomBtn?.backgroundColor = UIColor.white
        self.bgImageView?.addSubview(self.bottomBtn!)
        self.bottomBtn?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-12)
            make.height.equalTo(24)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-3)
        }
        
        self.self.bottomImageView = FLAnimatedImageView.init()
        let url2 =  Bundle.main.url(forResource: "home_platform_bottom_gif", withExtension: "gif")
        do{
            let data2 = try Data(contentsOf: url2!)
            let animatedImage2 = FLAnimatedImage.init(animatedGIFData: data2)
            self.bottomImageView?.animatedImage = animatedImage2
        }catch{}
       
        self.bottomBtn?.addSubview(self.bottomImageView!)
        self.bottomImageView?.snp.makeConstraints{(make) in
            make.width.equalTo(25)
            make.height.equalTo(14)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-10)
        }
//        self.lineView1 = UIView.init()
//        self.lineView1?.backgroundColor = HexRGBAlpha(0xffc569, 0.8)
//        self.bgImageView?.addSubview(self.lineView1!)
//        self.lineView1?.snp.makeConstraints{(make) in
//            make.left.equalToSuperview().offset(25)
//            make.height.equalTo(0.5)
//            make.width.equalTo(((self.width-10-12)/2)-25)
//            make.top.equalToSuperview().offset(self.height/3-6)
//        }
//
//        self.lineView2 = UIView.init()
//        self.lineView2?.backgroundColor = HexRGBAlpha(0xffc569, 0.8)
//        self.bgImageView?.addSubview(self.lineView2!)
//        self.lineView2?.snp.makeConstraints{(make) in
//            make.left.equalTo((self.lineView1?.snp.right)!) .offset(25)
//            make.height.equalTo(0.5)
//            make.right.equalToSuperview().offset(-25)
//            make.top.equalToSuperview().offset(self.height/3-6)
//        }
//
//        self.lineView3 = UIView.init()
//        self.lineView3?.backgroundColor = HexRGBAlpha(0xffc569, 0.8)
//        self.bgImageView?.addSubview(self.lineView3!)
//        self.lineView3?.snp.makeConstraints{(make) in
//            make.left.equalToSuperview().offset(25)
//            make.height.equalTo(0.5)
//            make.width.equalTo(((self.width-10-12)/2)-25)
//            make.top.equalToSuperview().offset(((self.height/3)*2)-6)
//        }
//
//        self.lineView4 = UIView.init()
//        self.lineView4?.backgroundColor = HexRGBAlpha(0xffc569, 0.8)
//        self.bgImageView?.addSubview(self.lineView4!)
//        self.lineView4?.snp.makeConstraints{(make) in
//            make.left.equalTo((self.lineView1?.snp.right)!) .offset(25)
//            make.height.equalTo(0.5)
//            make.right.equalToSuperview().offset(-25)
//            make.top.equalToSuperview().offset(((self.height/3)*2)-6)
//        }
//
//        self.lineViewY1 = UIView.init()
//        self.lineViewY1?.backgroundColor = HexRGBAlpha(0xffc569, 0.8)
//        self.bgImageView?.addSubview(self.lineViewY1!)
//        self.lineViewY1?.snp.makeConstraints{(make) in
//            make.centerX.equalToSuperview()
//            make.height.equalTo((self.height/3)-40)
//            make.top.equalToSuperview().offset(20)
//            make.width.equalTo(0.5)
//        }
//
//        self.lineViewY2 = UIView.init()
//        self.lineViewY2?.backgroundColor = HexRGBAlpha(0xffc569, 0.8)
//        self.bgImageView?.addSubview(self.lineViewY2!)
//        self.lineViewY2?.snp.makeConstraints{(make) in
//            make.centerX.equalToSuperview()
//            make.height.equalTo((self.height/3)-40)
//            make.top.equalToSuperview().offset((self.height/3)+20)
//            make.width.equalTo(0.5)
//        }
//
//        self.lineViewY3 = UIView.init()
//        self.lineViewY3?.backgroundColor = HexRGBAlpha(0xffc569, 0.8)
//        self.bgImageView?.addSubview(self.lineViewY3!)
//        self.lineViewY3?.snp.makeConstraints{(make) in
//            make.centerX.equalToSuperview()
//            make.height.equalTo((self.height/3)-40)
//            make.top.equalToSuperview().offset(((self.height/3)*2)+20)
//            make.width.equalTo(0.5)
//        }
    }
    
    //滚动到顶部
    @objc func actionToScrollTop() -> Void {
        self.collectionView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
    }
    
    //滚动到底部
    @objc func actionToScrollBottom() -> Void {
        self.collectionView.setContentOffset(CGPoint.init(x: 0, y: self.collectionView.contentSize.height-self.collectionView.height), animated: true)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.collectionView {
            if (self.collectionView.contentOffset.y <= 0)
            {
                //顶部
                print("到达顶部")
                self.topBtn?.isHidden = true
            }else{
                self.topBtn?.isHidden = false
            }
            
            if (self.collectionView.contentSize.height - self.collectionView.contentOffset.y-self.collectionView.frame.size.height <=  0)
            {
                //底部
                print("到达底部")
                self.bottomBtn?.isHidden = true
            }else{
                self.bottomBtn?.isHidden = false
            }
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayList?.count ?? 0
      //  return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: 0, height: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.init(width: 0, height: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        return UICollectionReusableView.init()
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:NewHomePlatformItemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "NewHomePlatformItemCell", for: indexPath) as! NewHomePlatformItemCell
        let dict:[String:Any] = self.arrayList?[indexPath.row] ?? [:]
        var isSelect = false
        if indexPath.row ==  self.selectIndex {
            isSelect = true
        }
        cell.setData(imgUrl: dict["f_imgNameIOS"] as? String ?? "", selImgUrl: dict["f_imgNameIOSed"] as? String ?? "", title: dict["f_name"] as? String ?? "", isSelect: isSelect,row:indexPath.row,count: arrayList?.count ?? 0)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        if newPlatformSeltBlock != nil {
            newPlatformSeltBlock!(self.arrayList?[self.selectIndex] ?? [:],self.selectIndex)
        }
        self.collectionView.reloadData()
    }
    
    func reload() -> Void {
        let platforms = GlobalInfo.platformInfoData
       
        self.arrayList =  platforms
        if isFirstRealod == false {
            self.bottomBtn?.isHidden = true
            if self.arrayList?.count ?? 0 <= 6 {
                self.bottomBtn?.isHidden = true
            }else{
                self.bottomBtn?.isHidden = false
            }
            self.isFirstRealod = true
        }
       
        self.collectionView.reloadData()
    }
    
}
